home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / utils time / DCal 0.5 / dcal.exe / dcal / source / dcal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-11  |  7.9 KB  |  438 lines

  1. /* Main code for Dcal */
  2.  
  3. /*
  4.     DCal, a Discordian calendar for the Palm OS.
  5.     Copyright (C) 1999 Ron Hale-Evans <rwhe@apocalypse.org>.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; the version used for this program
  10.     is Version 2.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  20.  
  21.     This program comes with ABSOLUTELY NO WARRANTY. Use it at your own risk.
  22.     For the text of the GNU General Public License, see the file gpl.txt,
  23.     which should have been distributed with this file.
  24. */
  25.  
  26.  
  27. #include <Pilot.h>
  28. #include "callback.h"
  29. #include "dcalRsc.h"
  30.  
  31.  
  32. // Globals
  33.  
  34. int dYear;
  35. int dMonth;
  36. int dDay;
  37. int dWeekday;
  38. int StTibsDayFlag;
  39. char TextToPrint [150];
  40.  
  41.  
  42. char DiscoMoNames[6][15] =
  43. {
  44.   "ERROR",
  45.   "Chaos",
  46.   "Discord",
  47.   "Confusion",
  48.   "Bureaucracy",
  49.   "Aftermath"
  50. };
  51.  
  52. char DiscoWeekdayNames[5][20] =
  53. {
  54.   "Sweetmorn",
  55.   "Boomtime",
  56.   "Pungenday",
  57.   "Prickle-Prickle",
  58.   "Setting Orange"
  59. };
  60.  
  61. char DiscoApostleHolyDays[6][15] = 
  62. {
  63.   "ERROR",
  64.   "Mungday",
  65.   "Mojoday",
  66.   "Syaday",
  67.   "Zaraday",
  68.   "Maladay"
  69. };
  70.  
  71. char DiscoSeasonHolyDays[6][20] =
  72. {
  73.   "ERROR",
  74.   "Chaoflux",
  75.   "Discoflux",
  76.   "Confuflux",
  77.   "Bureflux",
  78.   "Afflux"
  79. };
  80.  
  81. int DiscoMoLens[6][2];
  82.  
  83.  
  84. void DiscoDataInit (void)
  85. {
  86.   int i;
  87.   int j;
  88.  
  89.   for (i=1; i<=5; i++)
  90.     for (j=0; j<=1; j++)
  91.       {
  92.     DiscoMoLens[i][j] = 73;
  93.       };
  94.   DiscoMoLens[1][1] = 74;
  95.   DiscoMoLens[0][0] = 0;
  96.   DiscoMoLens[0][1] = 0;
  97. }
  98.  
  99.  
  100.  
  101. int GregPJDFromGregDate(int gYear, int gMonth, int gDay)
  102. {
  103.   ULong DaysForNewYears;
  104.   ULong DaysForGDate;
  105.   DateType NewYearsDate;
  106.   DateType gDate;
  107.  
  108.   NewYearsDate.year = gYear;
  109.   NewYearsDate.month = 1;
  110.   NewYearsDate.day = 1;
  111.  
  112.   gDate.year = gYear;
  113.   gDate.month = gMonth;
  114.   gDate.day = gDay;
  115.  
  116.   DaysForNewYears = DateToDays(NewYearsDate);
  117.   DaysForGDate = DateToDays(gDate);
  118.  
  119.   return (DaysForGDate - DaysForNewYears + 1);
  120. }
  121.  
  122.  
  123. int GregYearIsLeapYear (int year)
  124. {
  125.  return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
  126. }
  127.  
  128.  
  129.  
  130. void CalcDiscoDate (void)
  131. {
  132.   ULong TimeInSeconds;
  133.   DatePtr gDateP;
  134.   int LeapFlag;
  135.   int dPJD;
  136.   int AdjustedPJD;
  137.   int NextMonthStart;
  138.   int PrevDays = 0;
  139.   int i;
  140.  
  141.   gDateP = (DatePtr)MemPtrNew(sizeof(DateType));
  142.   TimeInSeconds = TimGetSeconds();
  143.   DateSecondsToDate (TimeInSeconds, gDateP);
  144.  
  145.   LeapFlag = 0;
  146.   if (GregYearIsLeapYear(gDateP->year))
  147.     LeapFlag = 1;
  148.   
  149.   dPJD = GregPJDFromGregDate(gDateP->year, gDateP->month, gDateP->day);
  150.   AdjustedPJD = dPJD;
  151.   if (LeapFlag)
  152.     {
  153.       if (dPJD == 60)
  154.       StTibsDayFlag = true;
  155.         else if (dPJD > 60)
  156.           AdjustedPJD = dPJD - 1;
  157.     };
  158.   dWeekday = (AdjustedPJD-1) % 5;
  159.  
  160.   dMonth = 0;
  161.   NextMonthStart = 1;
  162.   while (dMonth < 5)
  163.     {
  164.       NextMonthStart = NextMonthStart + DiscoMoLens[dMonth][LeapFlag];
  165.       if (AdjustedPJD >= NextMonthStart)
  166.     dMonth++;
  167.       else
  168.         break;
  169.     };
  170.  
  171.   for (i=1; i<=dMonth; i++)
  172.     {
  173.       PrevDays = PrevDays + DiscoMoLens[i-1][0];
  174.     };
  175.   dDay = AdjustedPJD - PrevDays;
  176.   
  177.   dYear = gDateP->year + 1166 + 1904;
  178.  
  179.   MemPtrFree (gDateP);
  180. }
  181.  
  182.  
  183. void MakeDiscoDateString (void)
  184. {
  185.   char SeasonStr [20];
  186.   char WeekdayStr [20];
  187.   char YearStr [20];
  188.   char DayStr [20];
  189.   char HolyDay [20];
  190.   int HolyDayFlag;
  191.  
  192.   StrCopy(SeasonStr,DiscoMoNames [dMonth]);
  193.   StrCopy (WeekdayStr, DiscoWeekdayNames [dWeekday]);
  194.   StrIToA (YearStr, dYear);
  195.  
  196.   HolyDayFlag = true;
  197.  
  198.   if (dDay == 5)
  199.     StrCopy (HolyDay, DiscoApostleHolyDays[dMonth]);
  200.       else if (dDay == 50)
  201.     StrCopy (HolyDay, DiscoSeasonHolyDays[dMonth]);
  202.       else
  203.         HolyDayFlag = false;
  204.  
  205.   if (StTibsDayFlag)
  206.   {
  207.     StrCopy (TextToPrint, "St. Tib's Day,\n");
  208.     StrCat (TextToPrint, YearStr);
  209.   }
  210.   else if (HolyDayFlag)
  211.     {
  212.       StrCopy (TextToPrint, HolyDay);
  213.       StrCat  (TextToPrint, ",\n");
  214.       StrCat  (TextToPrint, WeekdayStr);
  215.       StrCat  (TextToPrint, ",\n");
  216.       StrIToA (DayStr, dDay);
  217.       StrCat  (TextToPrint, DayStr);
  218.       StrCat  (TextToPrint, " ");
  219.       StrCat  (TextToPrint, SeasonStr);
  220.       StrCat  (TextToPrint, " ");
  221.       StrCat  (TextToPrint, YearStr);
  222.     }
  223.   else
  224.     {
  225.       StrCopy (TextToPrint, WeekdayStr);
  226.       StrCat  (TextToPrint, ",\n");
  227.       StrIToA (DayStr, dDay);
  228.       StrCat  (TextToPrint, DayStr);
  229.       StrCat  (TextToPrint, " ");
  230.       StrCat  (TextToPrint, SeasonStr);
  231.       StrCat  (TextToPrint, " ");
  232.       StrCat  (TextToPrint, YearStr);
  233.     };
  234. }
  235.  
  236.  
  237.  
  238.  
  239.  
  240. void ShowText (void)
  241. {
  242.   FormPtr frmP;
  243.   FieldPtr dstP;
  244.   CharPtr dstT;
  245.   VoidHand dstHandle;
  246.   
  247.   frmP = FrmGetActiveForm();
  248.   
  249.   dstP = (FieldPtr)(FrmGetObjectPtr(frmP, (FrmGetObjectIndex(frmP, MainTextField))));
  250.  
  251.   dstHandle = MemHandleNew(StrLen(TextToPrint)+1);
  252.   dstT = MemHandleLock (dstHandle);
  253.  
  254.   StrCopy (dstT, TextToPrint);
  255.   MemHandleUnlock (dstHandle);
  256.   FldSetTextHandle (dstP, (Handle)dstHandle);
  257.   FldDrawField (dstP);
  258. }
  259.  
  260.  
  261.  
  262. void DisplayDate (void)
  263. {
  264.   DiscoDataInit();
  265.   CalcDiscoDate();
  266.   MakeDiscoDateString();
  267.  
  268.   ShowText();
  269. }
  270.  
  271.  
  272.  
  273.  
  274. static Boolean AboutFormHandleEvent(EventPtr e)
  275. {
  276.     Boolean handled = false;
  277.     
  278.     CALLBACK_PROLOGUE
  279.  
  280.     switch (e->eType)
  281.     {
  282.         case frmOpenEvent:
  283.             FrmDrawForm(FrmGetActiveForm());
  284.             handled = true;
  285.             break;
  286.         case ctlSelectEvent:
  287.             if (e->data.popSelect.controlID == AboutOKButton)
  288.             {
  289.                 FrmReturnToForm(MainForm);
  290.                 handled = true;
  291.                 break;
  292.             }
  293.     }
  294.  
  295.     CALLBACK_EPILOGUE
  296.  
  297.     return(handled);
  298. }
  299.  
  300.  
  301.  
  302.  
  303.  
  304. static Boolean MainFormHandleEvent (EventPtr e)
  305. {
  306.     Boolean handled = false;
  307.     FormPtr frm;
  308.     
  309.     CALLBACK_PROLOGUE
  310.  
  311.     switch (e->eType) {
  312.     case frmOpenEvent:
  313.     frm = FrmGetActiveForm();
  314.     FrmDrawForm(frm);
  315.  
  316.     DisplayDate();
  317.  
  318.     handled = true;
  319.     break;
  320.  
  321.     case menuEvent:
  322.     MenuEraseStatus(NULL);
  323.  
  324.     switch(e->data.menu.itemID) 
  325.       {
  326.                 case MainOptionsAboutMenuItem:
  327.                     FrmPopupForm(AboutForm);
  328.                     handled = true;
  329.                     break;
  330.       }
  331.  
  332.         handled = true;
  333.     break;
  334.  
  335.     case ctlSelectEvent:
  336.     switch(e->data.ctlSelect.controlID) {
  337.     }
  338.     break;
  339.  
  340.     default:
  341.         break;
  342.     }
  343.  
  344.     CALLBACK_EPILOGUE
  345.  
  346.     return handled;
  347. }
  348.  
  349.  
  350.  
  351. static Boolean ApplicationHandleEvent(EventPtr e)
  352. {
  353.     FormPtr frm;
  354.     Word    formId;
  355.     Boolean handled = false;
  356.  
  357.     if (e->eType == frmLoadEvent) {
  358.     formId = e->data.frmLoad.formID;
  359.     frm = FrmInitForm(formId);
  360.     FrmSetActiveForm(frm);
  361.  
  362.     switch(formId) {
  363.     case MainForm:
  364.         FrmSetEventHandler(frm, MainFormHandleEvent);
  365.         break;
  366.     case AboutForm:
  367.         FrmSetEventHandler(frm, AboutFormHandleEvent);
  368.         break;
  369.     }
  370.     handled = true;
  371.     }
  372.  
  373.     return handled;
  374. }
  375.  
  376.  
  377.  
  378. /* Get preferences, open (or create) app database */
  379. static Word StartApplication(void)
  380. {
  381.     FrmGotoForm(MainForm);
  382.     return 0;
  383. }
  384.  
  385.  
  386.  
  387.  
  388. /* Save preferences, close forms, close app database */
  389. static void StopApplication(void)
  390. {
  391.     FrmSaveAllForms();
  392.     FrmCloseAllForms();
  393. }
  394.  
  395.  
  396.  
  397.  
  398. /* The main event loop */
  399. static void EventLoop(void)
  400. {
  401.     Word err;
  402.     EventType e;
  403.  
  404.     do {
  405.     EvtGetEvent(&e, evtWaitForever);
  406.     if (! SysHandleEvent (&e))
  407.         if (! MenuHandleEvent (NULL, &e, &err))
  408.         if (! ApplicationHandleEvent (&e))
  409.             FrmDispatchEvent (&e);
  410.     } while (e.eType != appStopEvent);
  411. }
  412.  
  413.  
  414.  
  415.  
  416. /* Main entry point; it is unlikely you will need to change this except to
  417.    handle other launch command codes */
  418. DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
  419. {
  420.     Word err;
  421.  
  422.     if (cmd == sysAppLaunchCmdNormalLaunch) {
  423.  
  424.     err = StartApplication();
  425.     if (err) return err;
  426.  
  427.     EventLoop();
  428.     StopApplication();
  429.  
  430.     } else {
  431.     return sysErrParamErr;
  432.     }
  433.  
  434.     return 0;
  435. }
  436.  
  437.  
  438.